package com.sky.controller.admin;

import com.sky.pojo.Merchant;
import com.sky.result.Result;
import com.sky.service.admin.MerchantService;
import com.sky.vo.ShopVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping
@Api(tags = "店铺相关接口")
public class ShopController {

    public static final String KEY = "SHOP_STATUS";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MerchantService merchantService;

    @PutMapping("/admin/shop/{status}")
    @ApiOperation("设置店铺营业状态")
    public Result setShopStatus(@PathVariable Short status) {
        log.info("设置店铺的营业状态为：{}", status == 1 ? "营业中" : "打烊中");
        redisTemplate.opsForValue().set(KEY, status);
        return Result.success();
    }

    @GetMapping("/admin/shop/status")
    @ApiOperation("获取店铺的营业状态")
    public Result<Integer> getShopStatus() {
        Integer status = (Integer) redisTemplate.opsForValue().get(KEY);
        return Result.success(status);
    }

    @PutMapping("/user/shop/{status}")
    @ApiOperation("设置店铺营业状态")
    public Result setUserShopStatus(@PathVariable Short status) {
        log.info("设置店铺的营业状态为：{}", status == 1 ? "营业中" : "打烊中");
        redisTemplate.opsForValue().set(KEY, status);
        return Result.success();
    }

    @GetMapping("/user/shop/status")
    @ApiOperation("获取店铺的营业状态")
    public Result<Integer> getUserShopStatus() {
        Integer status = (Integer) redisTemplate.opsForValue().get(KEY);
        return Result.success(status);
    }

    //  获取店铺电话
    @GetMapping("/getMerchantInfo")
    @ApiOperation("获取商家信息")
    public Result getMerchantInfo() {
        //  获取唯一商家对象信息
        Merchant merchant = merchantService.getShopById(1L);
        String shopName = merchant.getShopName();
        String shopAddress = merchant.getShopAddress();
        String shopTelephone = merchant.getShopTelephone();
        ShopVO build = ShopVO.builder().phone(shopTelephone).shopName(shopName).shopAddress(shopAddress).build();
        return Result.success(build);
    }
}
